package cn.kivensoft.util;

import java.util.Objects;

/** 键值对类型
 * @author kiven lee
 * @version 1.0
 * @since 2018-04-06
 */
public class KeyValue<K, V> {

	private K key;
	private V value;

	public KeyValue() { }

	public KeyValue(K key, V value) {
		this.key = key;
		this.value = value;
	}

	public static <K, V> KeyValue<K, V> of(K first, V second) {
		return new KeyValue<>(first, second);
	}

	final public KeyValue<K, V> sets(K first, V second) {
		this.key = first;
		this.value = second;
		return this;
	}

	final public K getKey() {
		return key;
	}

	final public void setKey(K key) {
		this.key = key;
	}

	final public V getValue() {
		return value;
	}

	final public void setValue(V value) {
		this.value = value;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((key == null) ? 0 : key.hashCode());
		result = prime * result + ((value == null) ? 0 : value.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null || getClass() != obj.getClass())
			return false;
		KeyValue<?, ?> other = (KeyValue<?, ?>) obj;
		return (key != null || other.key == null)
				&& Objects.equals(key, other.key)
				&& (value != null || other.value == null)
				&& Objects.equals(value, other.value);
	}

	@Override
	public String toString() {
		return "KeyValue {key: " + key + ", value: " + value + "}";
	}

}
